package com.rqf.ls.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rqf.common.base.ProjectInfor;
import com.rqf.ls.service.IManagementQuestion;
import com.rqf.ls.service.IQuestionAnswerService;
import com.rqf.ls.service.IManagementUserService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("questionMng")
public class ManagementQuestionController {
	
	@Autowired
	private IManagementUserService userManagementService;
	@Autowired
	private IQuestionAnswerService questionService;
	@Autowired
	private IManagementQuestion questionMngService;

	/**
	 * 问题列表
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年5月18日
	 */
	@RequestMapping("")
	public ModelAndView questionMng(HttpServletRequest request){
		ModelAndView mv = null;
		// 验证管理员权限
		String userName = SessionUtil.getStringAttri(request, "userName");
		String password = SessionUtil.getStringAttri(request, "password");
		Boolean vertified = userManagementService.verifySession(userName,
				password);
		if (vertified) {
			mv = new ModelAndView("manager/question/list");
		}else {
			mv = new ModelAndView("errors/noAuth");
		}
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("listData")
	public void listData(HttpServletRequest request, 
			HttpServletResponse response){
		RequestInput requestInput = new RequestInput(request);
		DataVector<Map<String, Object>> listData = questionMngService.getList(requestInput);
		List<Map<String, Object>> list = listData.getList();
		/*Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object idObj = next.get("id");
			if (idObj != null) {
				Integer answerCount = questionService.answerCount(Integer.parseInt(idObj.toString()));
				next.put("answerCount", answerCount);
			}
		}*/
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(list, null, null));
	}
	 /**
	 * 问题详细
	 * @param request
	 * @param id
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月18日
	 */
	@RequestMapping("detail/{id}/{phoneNo}")
	public ModelAndView detail(HttpServletRequest request,
			@PathVariable Integer id, @PathVariable String phoneNo){
		ModelAndView mv = new ModelAndView("manager/question/detail");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("id",id);
		mv.addObject("phoneNo",phoneNo);
		Map<String, Object> map = questionService.questionMap(id);
		mv.addAllObjects(map);
		return mv;
	}
	@RequestMapping("detailData/{id}")
	public void detailData(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer id){
		List<List<Map<String, Object>>> returnList = new ArrayList<List<Map<String,Object>>>();
		List<Map<String, Object>> answerList = questionService.answerList(id);
		Iterator<Map<String, Object>> iterator = answerList.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object idObj = next.get("ID");
			if (idObj != null) {
				List<Map<String, Object>> evaluationList = questionService.evaluationList(Integer.parseInt(idObj.toString()));
				returnList.add(evaluationList);
			}
		}
		String json = JSONSerielizer.toJson(returnList, null, null);
		OutputUtil.returnString(response, json);
	//	Map<String, Object> map = lawyerRegisterService.read(phoneNo);
	}
	@RequestMapping("date")
	public void date(HttpServletResponse response){
		List<Map<String, Object>> dateList = questionMngService.dateList();
		OutputUtil.returnString(response, JSONSerielizer.toJson(dateList, null, null));
	}
	
}
